package view;


import main.BarApplication;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class KeyboardDisplay extends JPanel implements ActionListener, Observer {
    JTextField tf;

    public KeyboardDisplay(JTextField tf) {
        super();
        this.tf=tf;
        setLayout(null);
    //  setBackground(new Color(151, 186, 225));
        setSize(190, 200);
        setLocation(570, 190);
        BarApplication.getBasket().addObserver(this);
        initComponents();
    }

    private void initComponents() {



        int btnOffset = 0;
        JButton btn1 = new JButton("1");
        btn1.setBounds(5, btnOffset, 60, 40);
        btn1.addActionListener(this);
        add(btn1);

        JButton btn2 = new JButton("2");
        btn2.setBounds(65, btnOffset, 60, 40);
        btn2.addActionListener(this);
        add(btn2);

        JButton btn3 = new JButton("3");
        btn3.setBounds(125, btnOffset, 60, 40);
        btn3.addActionListener(this);
        add(btn3);

        btnOffset=btnOffset+40;

        JButton btn4 = new JButton("4");
        btn4.setBounds(5, btnOffset, 60, 40);
        btn4.addActionListener(this);
        add(btn4);

        JButton btn5 = new JButton("5");
        btn5.setBounds(65, btnOffset, 60, 40);
        btn5.addActionListener(this);
        add(btn5);

        JButton btn6 = new JButton("6");
        btn6.setBounds(125, btnOffset, 60, 40);
        btn6.addActionListener(this);
        add(btn6);

        btnOffset=btnOffset+40;

        JButton btn7 = new JButton("7");
        btn7.setBounds(5, btnOffset, 60, 40);
        btn7.addActionListener(this);
        add(btn7);

        JButton btn8 = new JButton("8");
        btn8.setBounds(65, btnOffset, 60, 40);
        btn8.addActionListener(this);
        add(btn8);

        JButton btn9 = new JButton("9");
        btn9.setBounds(125, btnOffset, 60, 40);
        btn9.addActionListener(this);
        add(btn9);

        btnOffset=btnOffset+40;

        JButton btnBksp = new JButton("<-");
        btnBksp.setBounds(5, btnOffset, 60, 40);
        btnBksp.addActionListener(this);
        add(btnBksp);

        JButton btn0 = new JButton("0");
        btn0.setBounds(65, btnOffset, 60, 40);
        btn0.addActionListener(this);
        add(btn0);

        JButton btnClear = new JButton("Clear");
        btnClear.setBounds(125, btnOffset, 60, 40);
        btnClear.addActionListener(this);
        add(btnClear);

        
    }

    @Override
    public void update(Observable observable, Object arg) {
        initComponents();
        revalidate();
        repaint();
    }

    @Override
    public void actionPerformed(ActionEvent event) {
       // BarApplication.getInstance().showPanel(new Payment());
        JButton button = (JButton) event.getSource();
        if(button.getText().equals("<-")){
            tf.setText(tf.getText().substring(0, tf.getText().length()-1));

        } else if(button.getText().equals("Clear")){

        } else {
          tf.setText(tf.getText()+button.getText());
        }
    }

}
